import json
import time
from urllib.parse import unquote
from random import choice
import pprint
from fake_useragent import UserAgent
import re
from openpyxl import Workbook
import typing
import traceback
from typing import Union, Any, AnyStr
import datetime
import inspect
_regexs = {}
def encode_to_json(st: str):
    if st is not None:
        libs = st.split("&")
        d = {}
        for lib in libs:
            s = lib.split("=", 1)
            if len(s) == 2:
                d[s[0]] = unquote(s[1])
            else:
                d[s[0]] = ""
        return json.dumps(d)
    else:
        return None
def json_dumps(data, indent=None, sort_keys=False):
    if not data:
        return None

    elif not indent:
        # 最紧凑的json字符串
        return json.dumps(data, sort_keys=sort_keys, separators=(',', ':'))

    else:
        return json.dumps(data, indent=indent, sort_keys=sort_keys)

def encode_to_dict(st: str):
    d = {}
    if st is not None:
        libs = st.split("&")
        for lib in libs:
            s = lib.split("=", 1)
            if len(s) == 2:
                d[s[0]] = unquote(s[1])
            else:
                d[s[0]] = ""
    return d

    # from feapder.network.request import Request
    # import random

def get_UA():
    ua=UserAgent()
    return ua.random

def get_proxy():
    # proxies = {
    #     'http': f'http://ws:ws123@haproxy.iinti.cn:{random.randint(24000, 24400)}',
    #     'https': f'http://ws:ws123@haproxy.iinti.cn:{random.randint(24000, 24400)}'
    # }
    # _http_url = 'http://t11314328068328:ucfy4ox6@j582.kdltps.com:15818/'  # 包月

    tunnel = "q998.kdltps.com:15818"

    # 用户名和密码方式
    username = "t12654990591741"
    password = "p0mmvmh3"

    _http_url = "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}


    PROXIES = {
        'http': _http_url,
        'https': _http_url,
    }

    return PROXIES


def get_edg():
    edg = (
        "edge99",
        "edge101",
        "chrome100",
        "chrome101",
        "chrome104",
        "chrome107"
    )
    return choice(edg)
def remove_html(text):
    return re.sub("<[^>]+>", "", text)
def remove_unchinese(text):
    pattern = re.compile(r'[^\u4e00-\u9fa5()（）]')
    return re.sub(pattern, "", text)
def remove_chinese(text):
    pattern = "[\u4e00-\u9fff：:]+"  # 匹配中文字符的Unicode范围
    return re.sub(pattern, "", text)
def replace_bracket(string):
    string = string.replace('(', '（')
    string = string.replace(')', '）')
    return string

